package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.LikeTravelMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private LikeTravelMapper likeTravelMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public TravelContent getTravelContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }



    @Override
    public List<Travel> listAll() {
        return travelMapper.selectAll();
    }

    @Override
    public Travel selectStrategyByTitle(String title) {
        return travelMapper.selectStrategyByTitle(title);
    }

    @Override
    public List<String> searchTitle() {
        return travelMapper.searchTitle();
    }

    @Override
    public void updateBrowse(Long id) {
        //每次浏览加1
        travelMapper.updateBrowse(id);

    }


    @Override
    public void updateState(Travel travel) {
        travelMapper.updateState(travel);
    }

    @Override
    public Travel getTravelById(Long id) {
        TravelContent content = contentMapper.selectByPrimaryKey(id);
        Travel travel = travelMapper.selectByPrimaryKey(id);
        travel.setTravelContent(content);
        return travel;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId()==null){
            //添加游记
            travel.setCreateTime(new Date());
            travel.setLastUpdateTime(new Date());
            travelMapper.insert(travel);
            //添加内容
            Long id = travel.getId();
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(id);
            contentMapper.insert(travelContent);

        }else{
            travel.setLastUpdateTime(new Date());
            travelMapper.updateByPrimaryKey(travel);
        }
    }
    @Override
    public Integer queryLikeCount(Long travelId) {
        return likeTravelMapper.queryLikeCount(travelId);
    }

    @Override
    public Integer queryIfexist(Long travelId, Long userId) {
        return   likeTravelMapper.queryIfexist(travelId,userId);

    }

    @Override
    public void saveLike(Long userId, Long travelId ) {

        likeTravelMapper.insert(travelId,userId,new Date());
    }

    @Override
    public void deleteLike(Long userId, Long travelId) {
        likeTravelMapper.deleteByPrimaryKey(travelId,userId);
    }

    @Override
    public PageInfo<List<Map>> queryForLikeMe(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = likeTravelMapper.queryForLikeMe(qo);
        return new PageInfo(list);

    }
}
